package com.conorroche.taskmanagement;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
//import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ViewTaskActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_task);
		Log.d("ViewTaskActivity", "Line 19");
		name = (TextView) findViewById(R.id.view_task_name);
		location = (TextView) findViewById(R.id.view_task_location);
		notes = (TextView) findViewById(R.id.view_task_notes);
		importance = (TextView) findViewById(R.id.view_task_importance);
		time = (TextView) findViewById(R.id.view_task_time);
		date = (TextView) findViewById(R.id.view_task_date);
		
		Bundle extras = getIntent().getExtras();
		DBAdapter db = new DBAdapter(getApplicationContext());
		if(extras.containsKey("name")){
			db.open();
			task = db.getTaskByName(extras.getString("name"));
			db.close();
		}else if(extras.containsKey("id")){
			db.open();
			task = db.getTaskById(extras.getInt("id"));
			db.close();
		}
		else {
			Toast.makeText(getApplicationContext(), "Sorry this task cannot be viewed", Toast.LENGTH_SHORT).show();
			finish();
		}
		
		task.moveToFirst();
		id = task.getInt(task.getColumnIndex("_id"));
		name.setText(task.getString(task.getColumnIndex("name")));
		location.setText(task.getString(task.getColumnIndex("location")));
		notes.setText(task.getString(task.getColumnIndex("notes")));
		importance.setText(task.getString(task.getColumnIndex("importance")));
		time.setText(task.getString(task.getColumnIndex("time")));
		date.setText(task.getString(task.getColumnIndex("date")));
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_view_task, menu);
		return true;
	}
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        	case R.id.edit_task:
        		addTask = new Intent(getApplicationContext(), AddTask.class);
        		addTask.putExtra("edit", true);
        		addTask.putExtra("id", id);
        		addTask.putExtra("name", name.getText());
        		addTask.putExtra("location", location.getText());
        		addTask.putExtra("notes", notes.getText());
        		addTask.putExtra("importance", importance.getText());
        		addTask.putExtra("time", time.getText());
        		addTask.putExtra("date", date.getText());
        		addTask.putExtra("dateRemind", task.getInt(task.getColumnIndex("dateRemind")));
        		addTask.putExtra("locationRemind", task.getInt(task.getColumnIndex("locationRemind")));
            	startActivity(addTask);
            	return true;
        	case R.id.add_tag:
        		addTag = new Intent(getApplicationContext(), AddTag.class);
        		startActivity(addTag);
        		return true;
        	case R.id.menu_delete:
        		DBAdapter db = new DBAdapter(getApplicationContext());
        		db.open();
        		db.deleteTask(id);
        		db.close();
        		Toast.makeText(getApplicationContext(), "Task deleted", Toast.LENGTH_SHORT ).show();
        		finish();
        }
        return super.onOptionsItemSelected(item);
        
	}
	 private TextView name, location, notes, importance, time, date;
	 private Intent addTask, addTag;
	 private int id;
	 Cursor task;
}
